package org.languageguide.media;


public class HTML5MediaGenerator implements MediaGenerator {
	
	public static final String irJavascript = "<script src=\"/applets/irhtml5.js\"></script>\n";
	public static final String ir2Javascript = "<script src=\"/applets/ir2html5.js\"></script>\n";
	public static final String ir3Javascript = "<script src=\"ir3html5.js\"></script>\n";
	
	/*public static String[] generate(String language, String name, boolean premium){
		String[] media = new String[2];
		return media;
	}*/
	boolean hidef = false;
	
	public HTML5MediaGenerator(boolean hidef){
		this.hidef = hidef;
	}
	
	
	
	public String[] getIrTags(String soundFile){
		//soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.cutoffwma(soundFile);
		//soundFile = Utility.root + soundFile;
		String[] media = new String[2];
		media[0] = irJavascript;
		media[1] = getHTML5Tag(soundFile, false, false);
		return media;
	}
	
	
	
	
	
	public static String getHTML5Tag(String soundFile, boolean autostart, boolean hidef){
		return getHTML5Tag(soundFile, autostart, hidef, false);
	}
			
	
	public static String getHTML5Tag(String soundFile, boolean autostart, boolean hidef, boolean preview){
		System.out.println("" + preview);
		StringBuilder builder = new StringBuilder();
		if (autostart){
			if (hidef){
				if (!preview){
					builder.append("<audio id=\"player\" autoplay controls><source src=\"");
				}
				else{
					builder.append("<audio id=\"player\" autoplay controls><source src=\"/user");
				}
				builder.append(soundFile);
				builder.append("-hi.m4a\" /><source src=\"");
			    builder.append(soundFile);
				builder.append("-hi.ogg\" type=\"audio/ogg\" />Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.</audio>");
			}
			else{
				if (!preview){
					builder.append("<audio id=\"player\" autoplay controls><source src=\"");
				}
				else{
					builder.append("<audio id=\"player\" autoplay controls><source src=\"/user");
				}
				builder.append(soundFile);
				if (!preview){
					builder.append(".m4a\" /><source src=\"");
				}
				else{
					builder.append(".m4a\" /><source src=\"/user");
				}
				builder.append(soundFile);
				builder.append(".ogg\" type=\"audio/ogg\" />Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.</audio>");				
			}
		}
		else{
			if (hidef){
				builder.append("<audio id=\"player\" controls>\n<source src=\"");
				builder.append(soundFile);
				builder.append("-hi.m4a\" />\n<source src=\"");
				builder.append(soundFile);
				builder.append("-hi.ogg\" type=\"audio/ogg\" />\n ");
				builder.append("Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.");
				builder.append("</audio>");
			}
			else{
				if (!preview){	
					builder.append("<audio id=\"player\" controls><source src=\"");
				}
				else{
					builder.append("<audio id=\"player\" controls><source src=\"/user");
				}
				builder.append(soundFile);
				builder.append(".m4a\" /><source src=\"");
				builder.append(soundFile);
				builder.append(".ogg\" type=\"audio/ogg\" />");
				builder.append("Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.");				
				builder.append("</audio>");
			}
		}
		
		
		/*String audio;
		if (autostart){
			audio = "<audio id=\"player\" autoplay controls src=\"" + soundFile + "\"></audio>";
		}
		else audio = "<audio id=\"player\" controls src=\"" + soundFile + "\"></audio>";
		*/
		return builder.toString();
	}
	
	public String[] getIR3Video(String soundFile) {
		//soundFile = Utility.convertwma(soundFile);
		String[] media = new String[2];
		media[0] = ir3Javascript;
		media[1] = getHTML5VideoTag(soundFile, false, false);
		return media;
	}	

	public String getHTML5VideoTag(String soundFile, boolean autostart, boolean hidef){
		StringBuilder builder = new StringBuilder();
		if (autostart){
			builder.append("<video id=\"player\"  width=\"320\" height=\"240\" autoplay controls><source src=\"");
			builder.append(soundFile);
			builder.append(".mp4\" /> />Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.</video>");
		}
		else{
			builder.append("<video id=\"player\"  width=\"320\" height=\"240\" controls><source src=\"");
			builder.append(soundFile);
			builder.append(".mp4\" /> />Whoops, we thought your browser supported HTML5 audio and it doesn't. <a href=\"/user/report.jsp\">Click here</a> to report the issue.</video>");						
		}
		return builder.toString();
	}	

	
	public String getKey(){
		if (!hidef)
			return "20";
		else return "21";
	}
	
	public String getOldKey(){
		return "2";
	}

	@Override
	public String[] getIr(String soundFile) {
		soundFile = Utility.cutoffwma(soundFile);
		//soundFile = Utility.root2 + soundFile;//; + ".m4a";
		String[] media = new String[2];
		media[0] = irJavascript;
		media[1] = getHTML5Tag(soundFile,true, false);
		return media;
	}
	
	@Override
	public String[] getIR2(String soundFile) {
		//soundFile = Utility.convertwma(soundFile);

		System.out.println("hidef" + hidef);
		String[] media = new String[2];
		media[0] = ir2Javascript;
		System.out.println("soundFile" + soundFile);
		media[1] = getHTML5Tag(soundFile,false, false);
		return media;
	}
	
	public String[] getIR3(String soundFile) {
		//soundFile = Utility.convertwma(soundFile);

		System.out.println("hidef" + hidef);
		String[] media = new String[2];
		media[0] = ir3Javascript;
		System.out.println("soundFile" + soundFile);
		media[1] = getHTML5Tag(soundFile,false, false);
		return media;
	}	
	
	
	

	public boolean useSvg(){
		return true;
	}

	
}